package com.shadow.demo.dictionary.controller;

import com.shadow.demo.dictionary.common.ParseUtil;
import com.shadow.demo.dictionary.dto.DicDTO;
import com.shadow.demo.dictionary.dto.DicItemDTO;
import com.shadow.demo.dictionary.entity.DicEntity;
import com.shadow.demo.dictionary.entity.DicItemEntity;
import com.shadow.demo.dictionary.service.DicItemService;
import com.shadow.demo.dictionary.service.DicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Tag(name = "组件-数据字典")
@RestController
@RequestMapping("dic")
public class DicController {

    private static final Logger log = LoggerFactory.getLogger(DicController.class);
    private final DicService dicService;
    private final DicItemService dicItemService;

    DicController(DicService dicService, DicItemService dicItemService) {
        this.dicService = dicService;
        this.dicItemService = dicItemService;
    }

    @Operation(summary = "根据字典Id获取字典内容")
    @GetMapping("getDicById")
    public DicDTO getDicById(
            @Parameter(name = "dicId", description = "字典Id", required = true)
            @RequestParam(value = "dicId") String dicId
    ) {
        return dicService.getDicById(dicId);
    }

    @Operation(summary = "根据字典名称获取字典内容")
    @GetMapping("getDicByName")
    public DicDTO getDicByName(
            @Parameter(name = "dicName", description = "字典名称", required = true)
            @RequestParam(value = "dicName") String dicName
    ) {
        return dicService.getDicByName(dicName);
    }

    @Operation(summary = "根据字典名称数组获取字典列表")
    @GetMapping("listDicByNames")
    public List<DicDTO> listDicByNames(
            @Parameter(name = "dicNames", description = "字典名称数组", required = true)
            @RequestParam(value = "dicNames") List<String> dicNames
    ) {
        List<DicEntity> list = dicService.getDicByNameList(dicNames);
        return ParseUtil.parseList(DicDTO.class, list);
    }

    @Operation(summary = "根据字典Id获取字典项")
    @GetMapping("listDicItemByDicId")
    public List<DicItemDTO> listDicItemByDicId(
            @Parameter(name = "dicId", description = "字典Id", required = true)
            @RequestParam(value = "dicId") String dicId,
            @Parameter(name = "parentId", description = "字典项的父节点Id，根节点为/")
            @RequestParam(name = "parentId", defaultValue = "/") String parentId
    ) {
        DicEntity dic = dicService.getById(dicId);
        return dicService.listDicItem(dic, parentId);
    }

    @Operation(summary = "根据字典名称获取字典项")
    @GetMapping("listDicItemByDicName")
    public List<DicItemDTO> listDicItemByDicName(
            @Parameter(name = "dicName", description = "字典名称", required = true)
            @RequestParam(value = "dicName") String dicName,
            @Parameter(name = "parentId", description = "字典项的父节点Id，根节点为/")
            @RequestParam(name = "parentId", defaultValue = "/") String parentId
    ) {
        DicEntity dic = dicService.getDicByName(dicName, 1);
        return dicService.listDicItem(dic, parentId);
    }

    @Operation(summary = "根据字典名称获取字典项")
    @PostMapping("listDicItemByDicNameNew")
    public List<DicItemDTO> listDicItemByDicNameNew(
            @Parameter(name = "dicName", description = "字典名称", required = true)
            @RequestParam(value = "dicName") String dicName,
            @Parameter(name = "parentId", description = "字典项的父节点Id，根节点为/")
            @RequestParam(name = "parentId", defaultValue = "/") String parentId,
            @Parameter(name = "param", description = "参数查询传参key=名称，value=值")
            @RequestBody Map<String, Object> param
    ) {
        return dicService.listDicItemByDicNameNew(dicName, parentId, param);
    }

    @Operation(summary = "根据字典Id获取字典项")
    @GetMapping("treeDicItemByDicId")
    public List<DicItemDTO> treeDicItemByDicId(
            @Parameter(name = "dicId", description = "字典Id", required = true)
            @RequestParam(value = "dicId") String dicId,
            @Parameter(name = "parentId", description = "字典项的父节点Id，根节点为/")
            @RequestParam(name = "parentId", defaultValue = "/") String parentId
    ) {
        return dicService.treeDicItemByDicId(dicId, parentId);
    }

    @Operation(summary = "根据字典名称获取字典项")
    @GetMapping("treeDicItemByDicName")
    public List<DicItemDTO> treeDicItemByDicName(
            @Parameter(name = "dicName", description = "字典名称", required = true)
            @RequestParam(value = "dicName") String dicName,
            @Parameter(name = "parentId", description = "字典项的父节点Id，根节点为/")
            @RequestParam(name = "parentId", defaultValue = "/") String parentId
    ) {
        return dicService.treeDicItemByDicName(dicName, parentId);
    }

    @Operation(summary = "根据多个字典名称批量获取字典项")
    @PostMapping("treeDicItemsByDicNames")
    public Map<String, List<DicItemDTO>> treeDicItemsByDicNames(
            @Parameter(description = "projectId")
            @RequestParam(required = false) Long projectId,
            @Parameter(description = "字典名称数组", required = true)
            @RequestBody String[] dicNames
    ) {
        Map<String,Object> params = new HashMap<>();
        if (projectId != null){
            params.put("projectId", projectId);
        }
        Map<String, List<DicItemDTO>> map = new HashMap<>();
        for (String dicName : dicNames) {
            List<DicItemDTO> dicItems = dicService.listDicItemByDicNameNew(dicName, "/", params);
            map.put(dicName, dicItems);
        }
        return map;
    }

    @Operation(summary = "获取多个数据字典")
    @GetMapping("dicNamepathMulti")
    public List<DicDTO> dicNamepathMulti(
            @RequestParam("xpath") String xpath,
            @RequestParam(value = "fillDepth", required = false) Integer fillDepth
    ) {
        int temp = fillDepth == null ? -1 : fillDepth;
        return dicService.dicNamepathMulti(xpath,temp);
    }

    @Operation(summary = "根据多个字典ID批量获取字典项")
    @PostMapping("getDicItemByIds")
    public List<DicItemDTO> getDicItemByIds(List<String> dicIds) {
        List<DicItemEntity> dicItemEntityList = dicItemService.getDicItemByIds(dicIds);
        if (CollectionUtils.isNotEmpty(dicItemEntityList)) {
            return dicItemEntityList.stream()
                    .map(x -> ParseUtil.parseValue(DicItemDTO.class, x))
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Operation(summary = "根据字典名称获取字典项")
    @PostMapping("listDicItemByName")
    List<DicItemDTO> listDicItemByName(
            @Parameter(name = "dicName", description = "字典名称", required = true)
            @RequestParam(value = "dicName") String dicName,
            @Parameter(name = "dicItemNames", description = "字典项名称", required = true)
            @RequestParam(value = "dicItemNames") List<String> dicItemNames
    ) {
        List<DicItemEntity> list = dicItemService.listDicItemByName(dicName, dicItemNames);
        return ParseUtil.parseList(DicItemDTO.class, list);
    }

    @Operation(summary = "根据字典Id获取字典项列表")
    @GetMapping("listSubDicItem")
    public List<DicItemDTO> listSubDicItem(
            @Parameter(name = "dicId", description = "字典Id", required = true)
            @RequestParam(value = "dicId") String dicId,
            @Parameter(name = "parentId", description = "父节点Id")
            @RequestParam(value = "parentId", required = false) String parentId
    ) {
        List<DicItemEntity> list = dicItemService.listSubDicItem(dicId, parentId);
        return ParseUtil.parseList(DicItemDTO.class, list);
    }

    @Operation(summary = "根据字典名称获取字典项列表")
    @GetMapping("listSubDicItemByName")
    public List<DicItemDTO> listSubDicItemByName(
            @Parameter(name = "dicName", description = "字典名称", required = true)
            @RequestParam(value = "dicName") String dicName,
            @Parameter(name = "parentId", description = "父节点Id")
            @RequestParam(value = "parentId", required = false) String parentId
    ) {
        List<DicItemEntity> list = dicItemService.listSubDicItemByName(dicName, parentId);
        return ParseUtil.parseList(DicItemDTO.class, list);
    }

    @Operation(summary = "namepath")
    @GetMapping("namepath")
    public List<DicItemDTO> fromNamePath(
            @RequestParam("xpath") String xpath,
            @RequestParam(value = "fillDepth", required = false) Integer fillDepth) {
//        int temp = fillDepth == null ? -1 : fillDepth; return dicService.getDicItemByNamePath(xpath, temp)
        log.info(xpath);
        log.info("gg : {}", fillDepth);
        return null;
    }
}
